const Redis = require('ioredis');
const redis = new Redis({
	port: 6379, // Redis port
	host: process.env.NODE_ENV == 'dev' ? '127.0.0.1' : '----',
	password: process.env.NODE_ENV == 'dev' ? '' : '----',
      db:0,
});

async function setKey(key, value, time = null) {
	if (typeof value == 'object') {
		value = JSON.stringify(value);
	}

	await redis.set(key, value);

	// 过期时间
	if (time != null) {
		await redis.expire(key, time);
	}
}

function getKey(val) {
	return redis.get(val);
}

function delKey(key) {
	return redis.del(key);
}

module.exports = {
	setKey,
	getKey,
	delKey
};
